<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/10/18
 * Time: 11:32
 */

namespace core\lib;

use function core\p;

class ReflectionTool
{

    private static $instance = null;
    public $raw;
    private $instanced;
    private $clas;
    public $methods;
    private $propertis;
    private $parent_name;
    public $parent;

    /*
     * method temp
     */
    private $getmethod;
    private $pamenents;
    private $methodname;
    private $parmsnum;
    public $DocComment;

    private function __construct($clas)
    {
        $this->clas = new \ReflectionClass($clas);
        $this->cclass();
    }

    public static function Instance($clas){
        if(!self::$instance instanceof self){
            self::$instance = new self($clas);
        }
        return self::$instance;
    }

    private function cclass(){
        $this->methods = $this->clas->getMethods();
        $this->propertis = $this->clas->getProperties();
        $this->parent = $this->clas->getParentClass();
//        foreach ($this->clas->getParentClass() as $value){
//            $this->parent_name = $value;
//        }
    }

    public function Method($method){
        $this->getmethod = $this->clas->getMethod($method);
        $this->methodname = $this->getmethod->getName();
        $this->pamenents = $this->getmethod->getParameters();
        $this->parmsnum = $this->getmethod->getNumberOfParameters();
        $this->DocComment = $this->getmethod->getDocComment();
    }

    public function getParent($clas){
        $clas = new \ReflectionClass($clas);
        foreach ($clas->getParentClass() as $value){
            $parent_name = $value;
        }
        return $parent_name;
    }

    public function getmethod($method){
        if($method == ""){
            return $this->getmethod;
        }
        $md = $this->clas->getMethod($method);
        return $md;
    }

    public function getMethodName($method){
        if($method == ""){
            return $this->methodname;
        }
        return $this->getmethod($method)->getName();
    }

    public function getPamenents($method = ""){
        if($method == ""){
            return $this->pamenents;
        }
        $method = $this->getmethod($method);
        $pamenents = $method->getParameters();
        return $pamenents;
    }

    public function getPamenentsNums($method = ""){
        if($method === ""){
            return $this->parmsnum;
        }
        $method = $this->getmethod($method);
        $parmsnum = $method->getNumberOfParameters();
        return $parmsnum;
    }

    public function doccomment($method = ""){
        if($method == ""){
            return $this->DocComment;
        }
        $aa = [];
        $DocComment = $this->getmethod($method)->getDocComment();
        preg_match_all("/\@.*/",$DocComment,$res);
        foreach ($res[0] as $re) {
            $temp = substr($re,1);
            $temp = preg_split("/ /",$temp);
            $aa[trim($temp[0])]=trim($temp[1]);
        }
        return $aa;
    }

    public function Fillparms($arr = [],$method = ""){
        if($method == ""){
            $parms = $this->pamenents;
        }else{
            $parms = $this->getPamenents($method);
        }
        $aa = [];
        foreach ($parms as $v){
            if($v->getClass()) return false;
            $aa[(string)($v->name)] = null;
        }
        foreach ($arr as $item=>$value) {
            if (!in_array($item,array_keys($arr))) return false;;
            $aa[$item]=$value;
        }
        return array_values($aa);
    }
}

